<?php

namespace app\common\model\dynamic;


use addons\xyb\XypHelper;
use think\Model;


class Jobs extends Model
{

    // 表名
    protected $name = 'dynamic_jobs';

    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'integer';

    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = 'updatetime';
    protected $deleteTime = false;

    public static $grant_status_list = [
        1 => '进行中',
        2 => '已结束',
        3 => '已下架'
    ];
    public static $grant_method_list = [
        1 => '全部',
        2 => '指定区域',
        3 => '指定用户',
    ];

    protected $append = [
        'activity_info','create_date'
    ];

    public function getActivityInfoAttr($value, $data){
        if(isset($data['activity_id']) && $data['activity_id']){
            return Activity::where('id', $data['activity_id'])->value('name');
        } else {
            return '';
        }
    }

    public function getCreateDateAttr($value, $data){
        if(isset($data['createtime']) && $data['createtime']) {
            return date('Y-m-d H:i', $data['createtime']);
        }else {
            return '';
        }
    }
    public static function getJobsAdminLists(){

        $activity_id = request()->param('activity_id/a');
        $keyword = request()->param('keyword', '');
        $talk_id = request()->param('talk_id/a', '');
        $deadline = request()->param('deadline/a');
        $top_page = request()->param('top_page');
        $reward_method = request()->param('reward_method/a');
        $push_method = request()->param('push_method/a');
        $grant_status = request()->param('grant_status/a');
        $field = 'id,activity_id,grant_total,days_num,users_method,fu_reward_method,fu_reward_point,fu_youth_point,
        fu_reward_coupons_id,reward_method,reward_point,youth_point,reward_coupons_id,grant_status,createtime,total_users,
        progress_total,complete_total,talk_id,push_method,image_num,video_num,text_num,invitation_status,fu_task_status,
        fu_task_num,dynamic_status';
        $query = self::field($field);
        if ($activity_id = XypHelper::stringToArray($activity_id)) {
            $query->whereIn('activity_id', $activity_id);
        }
        if ($keyword) {
            $query->where(function ($query) use ($keyword) {
                $query->where('id', 'like', "%{$keyword}%")
                    ->whereOr('name', 'like', "%{$keyword}%");
            });
        }
        if ($reward_method = XypHelper::stringToArray($reward_method)) {
            $query->whereIn('reward_method', $reward_method);
        }

        if ($grant_status = XypHelper::stringToArray($grant_status)) {
            $query->whereIn('grant_status', $grant_status);
        }
        return $query->order('id desc')->paginate(10);
    }

    public static function getActivityJobsDetailsItems($activity_id)
    {
        $field = 'id,activity_id,grant_total,days_num,users_method,fu_reward_method,fu_reward_point,fu_youth_point,fu_reward_coupons_id,
        reward_method,reward_point,youth_point,reward_coupons_id,__key,talk_id,push_num,push_method,image_num,video_num,text_num,
        invitation_status,fu_task_status,fu_task_num,dynamic_status';
        $jobs_lists = self::field($field)->where('activity_id', $activity_id)->select();
        if(!$jobs_lists){
            return [];
        }
        foreach ($jobs_lists as $key => $val){
            $jobs_lists[$key]['fu_reward_coupons_id'] = XypHelper::formatJsonStringToArray($val['fu_reward_coupons_id']);
            $jobs_lists[$key]['reward_coupons_id'] = XypHelper::formatJsonStringToArray($val['reward_coupons_id']);
            $jobs_lists[$key]['talk_id'] =  array_map('intval',  XypHelper::formatJsonStringToArray($val['talk_id']));
        }
        return $jobs_lists;
    }


    public static function getJobsInfo($jobs_id){
        $field = 'id,activity_id,grant_total,days_num,users_method,fu_reward_method,fu_reward_point,fu_youth_point,fu_reward_coupons_id,
        reward_method,reward_point,youth_point,reward_coupons_id,__key,talk_id,push_num,push_method,image_num,video_num,text_num,
        invitation_status,fu_task_status,fu_task_num,push_num,dynamic_status,examine_method';
        $jobs_info = self::field($field)->where('id', $jobs_id)->find();
        if(!$jobs_info){
            return [];
        }
        $jobs_info['fu_reward_coupons_id'] = XypHelper::formatJsonStringToArray($jobs_info['fu_reward_coupons_id']);
        $jobs_info['reward_coupons_id'] = XypHelper::formatJsonStringToArray($jobs_info['reward_coupons_id']);
        return $jobs_info;
    }


    public static function getJobsInfoByActivity($activity_id){
        $field = 'id,activity_id,grant_total,days_num,users_method,fu_reward_method,fu_reward_point,fu_youth_point,fu_reward_coupons_id,
        reward_method,reward_point,youth_point,reward_coupons_id,__key,talk_id,push_num,push_method,image_num,video_num,text_num,
        invitation_status,fu_task_status,fu_task_num,dynamic_status';
        $jobs_info = self::field($field)->where('activity_id', $activity_id)->order('id','asc')->find();
        if(!$jobs_info){
            return [];
        }
        $jobs_info['fu_reward_coupons_id'] = XypHelper::formatJsonStringToArray($jobs_info['fu_reward_coupons_id']);
        $jobs_info['reward_coupons_id'] = XypHelper::formatJsonStringToArray($jobs_info['reward_coupons_id']);
        return $jobs_info;
    }

    public static function getNextJobsInfoByActivity($activity_id,$jobs_id){
        $field = 'id,activity_id,grant_total,days_num,users_method,fu_reward_method,fu_reward_point,fu_youth_point,fu_reward_coupons_id,
        reward_method,reward_point,youth_point,reward_coupons_id,__key,talk_id,push_num,push_method,image_num,video_num,text_num,
        invitation_status,fu_task_status,fu_task_num,dynamic_status';
        $jobs_info = self::field($field)->where('activity_id', $activity_id)
            ->where('id', '>', $jobs_id)->order('id','asc')->find();
        if(!$jobs_info){
            return [];
        }
        $jobs_info['fu_reward_coupons_id'] = XypHelper::formatJsonStringToArray($jobs_info['fu_reward_coupons_id']);
        $jobs_info['reward_coupons_id'] = XypHelper::formatJsonStringToArray($jobs_info['reward_coupons_id']);
        return $jobs_info;
    }
}